// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose:  Implementation of the CImplUpload class..
//   $Id$


#include "stdafx.h"
#include "upload.h"
#include "boundNode.h"
#include "namespaces.h"
#include "uploadUI.h"

const SQName CImplUpload::sm_arrQNAvailableTypes[UPLOAD_TYPE_COUNT]
=
{
	{ws_NSURI_XSD,L"hexBinary"},
	{ws_NSURI_XSD,L"base64Binary"},
	{ws_NSURI_XSD,L"anyURI"},
};

CImplUpload::CImplUpload(CXFormsElementUpload* pElement)
	:CImplFormControlInput((CXFormsElementFormControlInput*) pElement)
	,m_pFileName(0)
	,m_pMediaType(0)
{
	CFormControl::SetControlType(L"<upload>");
}

CImplUpload::~CImplUpload()
{
	delete m_pFileName;
	m_pFileName = 0;
	delete m_pMediaType;
	m_pMediaType = 0;
}

CImplMediaType::~CImplMediaType()
{
	dynamic_cast<CImplUpload*>(m_pParent)->m_pMediaType = 0;
}

CImplFileName::~CImplFileName()
{
	dynamic_cast<CImplUpload*>(m_pParent)->m_pFileName = 0;
}

void CImplUpload::buildDependencies()
{
	 __super::buildDependencies();
	 bool bIsCorrectlyBound = false;
	CVertex * pVx =0;
	 if(m_vertices.GetUpperBound() !=-1)
		pVx = m_vertices.GetAt(0);
	if(pVx)
	{
		CBoundNode *pBN = pVx->m_pInstanceNode;
		if(pBN)
		{
			bIsCorrectlyBound = pBN->isOneOfType(sm_arrQNAvailableTypes,UPLOAD_TYPE_COUNT);
		}
		
		if(!bIsCorrectlyBound)
		{
			//unbind
			if(pBN)
				pBN->RemoveTarget(this);
			m_vertices.RemoveAt(0);
			onBindToNothing();
		}
		else
		{
			m_bGetBinaryData = pBN->isOneOfType(sm_arrQNAvailableTypes,UPLOAD_TYPE_COUNT-1);
		}
	}
}


HRESULT CImplUpload::putValue(BSTR const  sValue)
{
	HRESULT hr = __super::putValue(sValue);
	if(m_pXFormsElement && m_pFileName || m_pMediaType)
	{
		CXFormsElementUpload * pUI = dynamic_cast<CXFormsElementUpload *>(m_pXFormsElement);
		if(pUI)
		{
			if(m_pFileName)
			{
					CComBSTR s;
					pUI->get_filename(&s);
					m_pFileName->putValue(s);
			}
			if(m_pMediaType)
			{
				CComBSTR s;
				pUI->get_mimeType(&s);
				m_pMediaType->putValue(s);
			}
		}
	}
	return hr;
}